package com.act.util;

import com.act.constant.BaseConst;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author JavisChen
 * @desc 文件处理工具类
 */
public class FileUtil {

    /**
     * 上传文件
     */
    public static void uploadFile(CommonsMultipartFile file, String fileName) {
        try {
            if (file.getSize() > 0) {
//                String path = BaseConst.UPLOAD.getPath();
                String path = BaseConst.UPLOAD.getPath();
                File targetFile = new File(path, fileName);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }
                file.transferTo(targetFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建文件名
     * @param file
     * @param name
     * @return
     */
    public static String createFileName(CommonsMultipartFile file, String name) {
        String[] s = file.getOriginalFilename().split("\\.");
        return name + "." + s[s.length - 1];
    }
}